<?php
/**[图片集表模型管理]
 * @Author: 976123967@qq.com
 * @Date:   2014-08-27 16:41:03
 * @Last Modified by:   chenryoku
 * @Last Modified time: 2014-11-16 11:38:42
 */
class PicModel extends Model{
	protected $tableName='article_pic';

	// 图集尺寸
	protected $small,$medium;
	// 初始化
	public function _initialize()
	{
		// 图集尺寸
		$this->small['width']='55';
		$this->small['height']='55';
		$this->medium['width']='300';
		$this->medium['height']='300';
	}



	/**
	 * [delete_pic 删除]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function delete_pic($id)
	{
		$field=$this->find($id);
		is_file($field['small']) && unlink($field['small']);
		is_file($field['medium']) && unlink($field['medium']);
		is_file($field['big']) && unlink($field['big']);
		$this->delete($id);
	}


	/**
	 * [get_all 获取图片]
	 * @param  [type] $aid [description]
	 * @return [type]      [description]
	 */
	public function get_all($aid)
	{
 		$data = $this->where(array('article_aid'=>$aid))->order('id asc')->	select();
 		if(!$data) return false;

 		// 组合路径
 		foreach ($data as $k=>$v)
 		{
 			$data[$k]['small'] = __ROOT__.'/'.$v['small'];
 			$data[$k]['medium'] = __ROOT__.'/'.$v['medium'];
 			$data[$k]['big'] = __ROOT__.'/'.$v['big'];
 		}
 		return $data;
	}



	/**
	 * [get_all_by_attr 获取图片]
	 * @param  [type] $aid [description]
	 * @return [type]      [description]
	 */
	public function get_all_by_attr($aid)
	{
		 $field = $this->get_all($aid);
		 if(!$field) return false;
		 $result = array();
		 $attrValueModel = D('AttrValue');
		 foreach($field as $v)
		 {
		 	// 属性值
		 	$attrValue = $attrValueModel->get_field($v['attr_value_attr_value_id'],'attr_value');
		 	

		 	$pics = $this->where(array('attr_value_attr_value_id'=>$v['attr_value_attr_value_id']))->field('attr_value_attr_value_id,article_aid',true)->order('id asc')->select();
		 	if($pics)
		 	{
		 		// 组合路径
		 		foreach ($pics as $key=>$value)
		 		{
		 			$pics[$key]['small'] = __ROOT__.'/'.$value['small'];
		 			$pics[$key]['medium'] = __ROOT__.'/'.$value['medium'];
		 			$pics[$key]['big'] = __ROOT__.'/'.$value['big'];
		 		}
		 	}

		 	$temp = array(
		 		'attr_value_id'=>$v['attr_value_attr_value_id'],
		 		'attr_value'=>$attrValue,
		 		'article_aid'=>$v['article_aid'],
		 		'pics'=>$pics,
		 	);
		 	$result[$v['attr_value_attr_value_id']] =$temp;
		 }
		 //p($result);die;
		 return $result;
	}




	/**
	 * [del_aid_pic 删除图片]
	 * @return [type] [description]
	 */
	public function del_aid_pic($aid)
	{
		$data = $this->where(array('article_aid'=>$aid))->select();
		if($data)
		{
			foreach ($data as $v) 
			{
				$this->delete_pic($v['id']);
			}
		}
	}



	/**
	 * [add_img 上传图片]
	 * @param [type] $aid [description]
	 */
	public function add_img($aid)
	{
		



        if(!empty($_FILES['img']['name']))
        {
            import('ORG.Net.UploadFile');
            $upload = new UploadFile();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $savePath ='./Data/Uploads/image/'.date('Y').'/'.date('m').'/'.date('d').'/';

            $picTitle = $_POST['pic_title'];
        
            
            is_dir($savePath) || mkdir($savePath,0777,true);
            if($info=$upload->uploadOne($_FILES['img'],$savePath)) 
            {   // 上传成功 获取上传文件信息
               

                // 获取比例
                $small_width = $this->small['width'];
                $small_height = $this->small['height'];
                $medium_width = $this->medium['width'];
                $medium_height = $this->medium['width'];

           

                foreach($info as $k=> $f)
                {
                    // 定义路径
                    $f['path']=$f['savepath'].$f['savename'];
                    $pic=pathinfo($f['path']);
                    // 缩略图
                    
                    $medium=$pic['dirname'] . '/' .$pic['filename']."_medium.".$pic['extension'];
                    $small = $pic['dirname'] . '/' .$pic['filename']."_small.".$pic['extension'];
                    import("ORG.Util.Image");
                    Image::thumb2($f['path'],$medium,$pic["extension"],$medium_width,$medium_height);
                    Image::thumb2($f['path'],$small,$pic["extension"],$small_width,$small_height);
                    $this->add(array(
                        'article_aid'=> $aid,
                        'big'=>$f['path'],
                        'medium'=>$medium,
                        'small'=>$small,
                        'pic_title'=>$picTitle[$k]

                    ));

     
                    
                }
            }

        }
	}


	/**
	 * [add_attr_img 添加属性图片集]
	 * @param [type] $aid [description]
	 */
	public function add_attr_img($aid)
	{
		$articleAttr = $_POST['article_attr'];
		//p($articleAttr);die;

		$attrModel = D('Attr');
		$attrValueModel = D('AttrValue');
		foreach($articleAttr as $k=>$v)
		{
			$temp = explode('-', $k);

			// 验证当前是否是有图片集的属性
			if(!$attrModel->check_is_attr_pic($temp[0]))
				continue;

			import('ORG.Net.UploadFile');
            $upload = new UploadFile();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $savePath ='./Data/Uploads/image/'.date('Y').'/'.date('m').'/'.date('d').'/';


			foreach ($v as  $value) 
			{
				
				$attrValueId = $attrValueModel->get_field_by_attr_value($value,'attr_value_id');
				//p($temp);
				//p($_FILES['img'.$attrValueId]);
				if(!empty($_FILES['img'.$attrValueId]['name']))
		        {
		            
		            $picTitle = $_POST['pic_title'.$attrValueId];
		        
		            
		            is_dir($savePath) || mkdir($savePath,0777,true);
		            if($info=$upload->uploadOne($_FILES['img'.$attrValueId],$savePath)) 
		            {   // 上传成功 获取上传文件信息
		               
		            	//p($info);
		                // 获取比例
		                $small_width = $this->small['width'];
                		$small_height = $this->small['height'];
                		$medium_width = $this->medium['width'];
                		$medium_height = $this->medium['width'];

		           

		                foreach($info as $k=> $f)
		                {
		                    // 定义路径
		                    $f['path']=$f['savepath'].$f['savename'];
		                    $pic=pathinfo($f['path']);
		                    // 缩略图
		                    
		                    $medium=$pic['dirname'] . '/' .$pic['filename']."_medium.".$pic['extension'];
		                    $small = $pic['dirname'] . '/' .$pic['filename']."_small.".$pic['extension'];
		                    import("ORG.Util.Image");
		                    Image::thumb2($f['path'],$medium,$pic["extension"],$medium_width,$medium_height);
		                    Image::thumb2($f['path'],$small,$pic["extension"],$small_width,$small_height);
		                    $this->add(array(
		                        'article_aid'=> $aid,
		                        'big'=>$f['path'],
		                        'medium'=>$medium,
		                        'small'=>$small,
		                        'pic_title'=>$picTitle[$k],
		                        'attr_value_attr_value_id'=>$attrValueId

		                    ));  
		                }
		            }


		        }
				
			}	
		}
	}

	/**
	 * [delete_pic_by_attrValueId 删除图片集合通过属性值关联外键]
	 * @return [type] [description]
	 */
	public function delete_pic_by_attrValueId($attrValueId)
	{
		$where = array('attr_value_attr_value_id'=>$attrValueId);
		$field = $this->where($where)->select();
		//p($field);
		// 删除文件
		if($field)
		{
			foreach($field as $v)
			{
				is_file($v['small']) && unlink($v['small']);
				is_file($v['medium']) && unlink($v['medium']);
				is_file($v['big']) && unlink($v['big']);
			}
		}
		// 删除数据库中数据
		$this->where($where)->delete();
		// 删除文档和属性的中间表数据
		M('article_attr')->where($where)->delete();
	}

}